package com.xiaobingby.admin.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaobingby.admin.sys.dto.RolePermissionDto;
import com.xiaobingby.admin.sys.entity.Role;
import com.xiaobingby.admin.sys.service.IPermissionService;
import com.xiaobingby.admin.sys.service.IRolePermissionService;
import com.xiaobingby.admin.sys.service.IRoleService;
import com.xiaobingby.admin.sys.vo.PermissionTreeVo;
import com.xiaobingby.common.controller.BaseController;
import com.xiaobingby.common.util.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * sys_role 角色表 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2018-10-29
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController<IRoleService, Role, Long> {

    @Autowired
    private IRoleService iRoleService;

    @Autowired
    private IPermissionService iPermissionService;

    @Autowired
    private IRolePermissionService iRolePermissionService;

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @ResponseBody
    @PostMapping
    public R addRole(@RequestBody Role role) {
        boolean save = iRoleService.save(role);
        return R.ok(save);
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @ResponseBody
    @PutMapping
    public R updateRole(@RequestBody Role role) {
        boolean save = iRoleService.updateById(role);
        return R.ok(save);
    }

    /**
     * 删除角色
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @DeleteMapping("{ids}")
    public R delPermission(@PathVariable Long[] ids) {
        boolean result = iRoleService.deleteRoleUserPermission(ids);
        return R.ok(result);
    }

    /**
     * 获取角色拥有权限
     *
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/permissionTree/{id}")
    public R<List<PermissionTreeVo>> getRolePermissionTree(@PathVariable Long id) {
        List<PermissionTreeVo> userRolePermissionTree = iPermissionService.getRolePermissionTree(id);
        return R.ok(userRolePermissionTree);
    }

    /**
     * 角色分配权限
     *
     * @param roleId
     * @param permIds
     * @return
     */
    @ResponseBody
    @PostMapping("/updateRolePermission")
    public R updateRolePermission(@RequestBody RolePermissionDto rolePermissionDto) {
        boolean b = iRolePermissionService.updateRolePermission(rolePermissionDto);
        return R.ok();
    }

    /**
     * 查询角色
     * @return
     */
    @ResponseBody
    @GetMapping("/queryRole")
    public R queryRole(String roleName) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(roleName)) {
            queryWrapper.like("name", roleName);
        }
        List<Role> list = iRoleService.list(queryWrapper);
        return R.ok(list);
    }

}
